import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        VisitStats stats = new VisitStats();
        Scanner scanner = new Scanner(System.in);

        while (true) {
            System.out.println("\n<网页访问统计系统 >");
            System.out.println("1. 记录页面访问");
            System.out.println("2. 查看访问量排行榜");
            System.out.println("3. 统计总访问量");
            System.out.println("4. 计算平均访问量");
            System.out.println("5. 查看页面最后访问时间");
            System.out.println("6. 清空访问统计");
            System.out.println("7. 退出系统");
            System.out.print("请选择操作：");
            int choice = scanner.nextInt();
            scanner.nextLine(); // 清除换行符

            switch (choice) {
                case 1:
                    System.out.print("请输入访问的页面URL：");
                    String url = scanner.nextLine();
                    stats.recordPageVisit(url);
                    System.out.println("访问记录已更新");
                    break;

                case 2:
                    stats.showVisitRanking();
                    break;

                case 3:
                    System.out.println("总访问量：" + stats.getTotalVisitCount());
                    break;

                case 4:
                    System.out.println("平均访问量：" + stats.getAverageVisitCount());
                    break;

                case 5:
                    System.out.print("请输入页面URL查看最后访问时间：");
                    String checkUrl = scanner.nextLine();
                    stats.showLastAccessTime(checkUrl);
                    break;

                case 6:
                    stats.clearStats();
                    break;

                case 7:
                    System.out.println("已退出系统");
                    scanner.close();
                    return;

                default:
                    System.out.println("无效选择，请重新选择！");
            }
        }
    }
}
